package com.example.myapplication.Outpatientappointment;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.ClientBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ModifyclientActivity extends AppCompatActivity {

    private ClientBean.RowsBean clientBean;
    private Button but_modifyclient;
    private Button but_kesi;
    private Button but_sava;
    private EditText ed_name;
    private EditText ed_sfz;
    private EditText ed_phone;
    private EditText ed_address;
    private RadioGroup item_radioGroupGender;
    private RadioButton iten_radioButtonMale;
    private RadioButton item_radioButtonFemale;
    private Gson gson;
    private Handler handler;
    private OkHttpClient okHttpClient;
    private Request request;
    private RequestBody requestBody;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_generator);
        Intent intent = getIntent();
        String clientBeanID = intent.getStringExtra("ClientBeanID");
        Gson gson = new Gson();
        clientBean = gson.fromJson(clientBeanID, ClientBean.RowsBean.class);

        initview();
        //完成信息的初始化
        initializePen();
        //返回
        back();
        //修改信息
        modifyclienpenson();
        //点击进入科室
        clicktdepartment();
    }

    //点击进入科室
    private void clicktdepartment() {
        but_kesi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ModifyclientActivity.this, ClicktdepartmentActivity.class);
                ModifyclientActivity.this.startActivity(intent);
            }
        });
    }



    private void modifyclienpenson() {
        but_sava.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject jsonObject = new JSONObject();
                String finalSexName = "x";
                int checkedRadioButtonId = item_radioGroupGender.getCheckedRadioButtonId();
                if(R.id.iten_radioButtonMale==checkedRadioButtonId){
                    finalSexName="0";
                }else if(R.id.item_radioButtonFemale==checkedRadioButtonId){
                    finalSexName="1";
                }
                try {
                    jsonObject.put("name",ed_name.getText().toString());
                    jsonObject.put("id",String.valueOf(clientBean.getId()));
                    jsonObject.put("sex", finalSexName);
                    jsonObject.put("cardId",ed_sfz.getText().toString());
                    jsonObject.put("tel",ed_phone.getText().toString());
                    jsonObject.put("address",ed_address.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                requestBody = RequestBody.create(jsonObject.toString(),JSON);
                            request = new Request.Builder()
                                    .url(TokenBean.IP+"/prod-api/api/hospital/patient")
                                    .header("Authorization",TokenBean.token)
                                    .put(requestBody)
                                    .build();
                            okHttpClient.newCall(request).enqueue(new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(ModifyclientActivity.this.getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
                                        }
                                    }) ;
                                }
                            });
            }
        });

    }

    private void back() {
        but_modifyclient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModifyclientActivity.this.finish();
            }
        });
    }

    private void initializePen() {
        if(clientBean.getName()==null){
            ed_name.setText("请输入姓名: ");
        }else  if(clientBean.getCardId()==null){
            ed_sfz.setText("请输入身份证号: ");
        }else  if(clientBean.getTel()==null){
            ed_phone.setText("请输入手机号: ");
        }else  if(clientBean.getAddress()==null){
            ed_address.setText("地址: ");
        }
        ed_name.setText(clientBean.getName());
        ed_sfz.setText(clientBean.getCardId());
        ed_phone.setText(clientBean.getTel());
        ed_address.setText(clientBean.getAddress());

        if(clientBean.getSex() ==null){

        }else {
            if(clientBean.getSex().equals("0")){
                iten_radioButtonMale.setChecked(true);
            }else  if(clientBean.getSex().equals("1")){
                item_radioButtonFemale.setChecked(true);
            }
        }
    }

    private void initview() {
        but_modifyclient = findViewById(R.id.but_modifyclient);
        but_kesi = findViewById(R.id.but_kesi);
        but_sava = findViewById(R.id.but_sava);
        ed_name = findViewById(R.id.ed_name);
        ed_sfz = findViewById(R.id.ed_sfz);
        ed_phone = findViewById(R.id.ed_phone);
        ed_address = findViewById(R.id.ed_address);
        item_radioGroupGender = findViewById(R.id.item_radioGroupGender);
        iten_radioButtonMale = findViewById(R.id.iten_radioButtonMale);
        item_radioButtonFemale = findViewById(R.id.item_radioButtonFemale);


        gson = new Gson();
        handler = new Handler();
        okHttpClient = new OkHttpClient();
    }
}